package com.zretc.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zretc.entity.Admin;
import com.zretc.service.AdminService;
import com.zretc.service.impl.AdminServiceImpl;
import com.zretc.tools.MD5Util;

/**
 * 管理员
 * Servlet implementation class AdminLoginServlet
 */
@WebServlet("/AdminLoginServlet")
public class AdminLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	AdminService as = new AdminServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 2 获取op的值
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		System.out.println("op:" + op);

		if ("adminlogin".equals(op)) {
			adminlogin(request, response);
		} else if ("add".equals(op)) {
			addadmin(request, response);
		}
	}

	/**
	 *  管理员登录
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void adminlogin(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取前台传递过来的查询的关键字
		String adminname = request.getParameter("adminname");
		String adminpwd = request.getParameter("adminpwd");
		System.out.println("adminname:" + adminname + ",adminpwd:" + adminpwd);
		Admin admin = as.adminlogin(adminname, adminpwd);
		if (admin != null) {
			request.getSession().setAttribute("Admin", admin);
		}
		if (admin != null) {
			// 转发
			request.getRequestDispatcher("fdtypepage.jsp").forward(request, response);
		} else {

		}
	}

	/**
	 *  管理员注册
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void addadmin(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		response.setContentType("text/html;charset=utf-8");
		String adminname = request.getParameter("adminname");
		String adminpwd = request.getParameter("adminpwd");
		String md5Pwd = MD5Util.getEncodeByMd5(adminpwd);
		String readminpwd = request.getParameter("readminpwd");
		if (adminpwd.equals(readminpwd)) {
			Admin admin = new Admin(0, adminname, md5Pwd);
			boolean flag = as.addAdmin(admin);
			if (flag) {
				request.getRequestDispatcher("adminlogin.jsp").forward(request, response);
			} else {
				response.getWriter().print("<script>alert('注册失败');location.href='addadmin.jsp'</script>");
			}
		} else {
			response.getWriter().print("<script>alert('两次输入密码不一致');location.href='addadmin.jsp'</script>");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
